﻿using Microsoft.AspNetCore.Mvc;
using System.Web.Http.Filters;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Web;
using Newtonsoft.Json.Linq;
using System.IO;
using hnrzxt.Models;
using Nancy.Json;

namespace hnrzxt.Controllers
{
    public class UploadController : Controller
    {
        private readonly IWebHostEnvironment _hostingEnvironment;

        public async Task<IActionResult> UploadFiles(List<IFormFile> file)
        {

            Zlxx editorResult = new Zlxx();
            foreach (var formFile in file)
            {
                if (formFile.Length > 0)
                {
                    FileInfo fi = new FileInfo(formFile.FileName);
                    string ext = fi.Extension;
                    var orgFileName = fi.Name;
                    var newFileName = Guid.NewGuid() + ext;
                    var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "你想要上传到文件夹");
                    var filePath = Path.Combine(uploads, newFileName);
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }
                    editorResult.Msb = "";
                }
                else
                {

                    editorResult.Msb = "";
                }
            }
            JavaScriptSerializer jss = new JavaScriptSerializer();
            string data = jss.Serialize(editorResult);//转换为Json格式!
            return Json(data);
        }
        //[HttpPost("UploadFile")]
        //public async Task<IActionResult> Post(List<IFormFile> files)
        //{
        //    long size = files.Sum(f => f.Length);

        //    // full path to file in temp location
        //    var filePath = Path.GetTempFileName();

        //    foreach (var formFile in files)
        //    {
        //        if (formFile.Length > 0)
        //        {
        //            using (var stream = new FileStream(filePath, FileMode.Create))
        //            {
        //                await formFile.CopyToAsync(stream);
        //            }
        //        }
        //    }

        //    // process uploaded files
        //    // Don't rely on or trust the FileName property without validation.

        //    return Ok(new { count = files.Count, size, filePath });
        //}

        //    [HttpPost]
        //    public JsonResult MultiFileUpload(IFormCollection formCollection)
        //    {
        //        //string upload =new 
        //        var currentDate = DateTime.Now;
        //        var webRootPath = _webHostingEnvironment.WebRootPath;//>>>相当于HttpContext.Current.Server.MapPath("") 
        //        var uploadFileRequestList = new List<UploadFileRequest>();
        //        try
        //        {
        //            //FormCollection转化为FormFileCollection
        //            var files = (FormFileCollection)formCollection.Files;

        //            if (files.Any())
        //            {
        //                foreach (var file in files)
        //                {
        //                    var uploadFileRequest = new UploadFileRequest();

        //                    var filePath = $"/UploadFile/{currentDate:yyyyMMdd}/";

        //                    //创建每日存储文件夹
        //                    if (!Directory.Exists(webRootPath + filePath))
        //                    {
        //                        Directory.CreateDirectory(webRootPath + filePath);
        //                    }

        //                    //文件后缀
        //                    var fileExtension = Path.GetExtension(file.FileName);//获取文件格式，拓展名

        //                    //判断文件大小
        //                    var fileSize = file.Length;

        //                    if (fileSize > 1024 * 1024 * 10) //10M TODO:(1mb=1024X1024b)
        //                    {
        //                        continue;
        //                    }

        //                    //保存的文件名称(以名称和保存时间命名)
        //                    var saveName = file.FileName.Substring(0, file.FileName.LastIndexOf('.')) + "_" + currentDate.ToString("HHmmss") + fileExtension;

        //                    //文件保存
        //                    using (var fs = System.IO.File.Create(webRootPath + filePath + saveName))
        //                    {
        //                        file.CopyTo(fs);
        //                        fs.Flush();
        //                    }

        //                    //完整的文件路径
        //                    var completeFilePath = Path.Combine(filePath, saveName);

        //                    uploadFileRequestList.Add(new UploadFileRequest()
        //                    {
        //                        FileName = saveName,
        //                        FilePath = completeFilePath
        //                    });
        //                }
        //            }
        //            else
        //            {
        //                return new JsonResult(new { isSuccess = false, resultMsg = "上传失败，未检测上传的文件信息~" });
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            return new JsonResult(new { isSuccess = false, resultMsg = "文件保存失败，异常信息为：" + ex.Message });
        //        }

        //        if (uploadFileRequestList.Any())
        //        {
        //            return new JsonResult(new { isSuccess = true, returnMsg = "上传成功", filePathArray = uploadFileRequestList });
        //        }
        //        else
        //        {
        //            return new JsonResult(new { isSuccess = false, resultMsg = "网络打瞌睡了，文件保存失败" });
        //        }

        //    }
    }
}
